<?php
require_once 'header.php';

// 获取所有商品
$products = getAllProducts();
?>

<div class="container">
    <h1 class="mb-4">电影票</h1>
    
    <div class="row mb-4">
        <div class="col-md-12">
            <div class="card">
                <div class="card-body">
                    <form class="form-inline">
                        <div class="form-group mb-2 mr-3">
                            <label for="search" class="sr-only">搜索</label>
                            <input type="text" class="form-control" id="search" name="search" placeholder="搜索电影票...">
                        </div>
                        <button type="submit" class="btn btn-primary mb-2">搜索</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
    
    <div class="row">
        <?php if (count($products) > 0): ?>
            <?php foreach($products as $product): ?>
                <div class="col-md-4 col-sm-6 mb-4">
                    <div class="card product-card">
                        <img src="<?php echo !empty($product['image']) ? $product['image'] : 'https://via.placeholder.com/300x200?text=电影票'; ?>" class="card-img-top product-image" alt="<?php echo $product['name']; ?>">
                        <div class="card-body">
                            <h5 class="card-title"><?php echo $product['name']; ?></h5>
                            <p class="card-text"><?php echo mb_substr($product['description'], 0, 60, 'UTF-8') . (mb_strlen($product['description'], 'UTF-8') > 60 ? '...' : ''); ?></p>
                            <div class="d-flex justify-content-between align-items-center">
                                <span class="text-danger font-weight-bold">¥<?php echo number_format($product['price'], 2); ?></span>
                                <a href="product.php?id=<?php echo $product['id']; ?>" class="btn btn-sm btn-outline-primary">查看详情</a>
                            </div>
                        </div>
                        <div class="card-footer bg-white">
                            <form action="add_to_cart.php" method="post">
                                <input type="hidden" name="product_id" value="<?php echo $product['id']; ?>">
                                <button type="submit" class="btn btn-block btn-primary">
                                    <i class="fas fa-shopping-cart mr-2"></i>加入购物车
                                </button>
                            </form>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        <?php else: ?>
            <div class="col-12">
                <div class="alert alert-info" role="alert">
                    暂无商品可供购买
                </div>
            </div>
        <?php endif; ?>
    </div>
    
    <nav aria-label="Page navigation" class="mt-4">
        <ul class="pagination justify-content-center">
            <li class="page-item disabled">
                <a class="page-link" href="#" tabindex="-1" aria-disabled="true">上一页</a>
            </li>
            <li class="page-item active"><a class="page-link" href="#">1</a></li>
            <li class="page-item"><a class="page-link" href="#">2</a></li>
            <li class="page-item"><a class="page-link" href="#">3</a></li>
            <li class="page-item">
                <a class="page-link" href="#">下一页</a>
            </li>
        </ul>
    </nav>
</div>

<?php require_once 'footer.php'; ?> 